<template>
    <div class="">
        <van-sticky>
            <van-nav-bar title="订单详情" left-text="返回" left-arrow @click-left="$router.go(-1)" />
        </van-sticky>


        <div>
            <van-address-list v-model="chosenAddressId" :list="list" default-tag-text="默认"
                @add="onAdd" @edit="onEdit" @select="onSelect" />
        </div>


    </div>
</template>
<script>
import Vue from 'vue';
import request from '../../utils/request'
import { NavBar } from 'vant';
import { Sticky } from 'vant';
import { Card } from 'vant';
import { Button } from 'vant';
import { AddressList } from 'vant';
import { AddressEdit } from 'vant';

Vue.use(AddressEdit);
Vue.use(AddressList);
Vue.use(Button);
Vue.use(Card);
Vue.use(Sticky);
Vue.use(NavBar);

export default {
    data() {
        return {
            chosenAddressId: '1',
            list: [],
        }
    },
    created() {
        this.getaddrList();
    },
    methods: {
        onAdd() {
            this.$router.push({
                path: '/address/add'
            })
        },
        onEdit(item, index) {
            this.$router.push({
                path: '/address/add',
                query: {
                    id: item.addressId
                }
            })
        },
        onSelect(item) {
            let addr = {
                id: item.addressId,
                name: item.name,
                tel: item.tel,
                address: item.address,
            };
            this.$store.commit('set_addr',addr);
            this.$router.go(-1)
        },
        getaddrList() {
            request.get('/address/all').then(res => {
                let newList = res.list.map(item => {
                    const address = item.province + item.city + item.county + item.addressDetail;
                    return {
                        ...item,
                        address: address,
                        id: item.addressId,
                    }
                })
                this.list = newList;
                const isDefaultList = newList.find(item => item.isDefault);
                if (isDefaultList) {
                    this.chosenAddressId = isDefaultList.addressId
                } else {
                    console.log(newList[0].addressId)
                    this.chosenAddressId = newList[0].addressId
                }

            })
        },
        getaddrAdd() {
            let params = {
                name: "姓名",
                tel: "手机号码",
                country: "",
                province: "省份",
                city: "城市",
                county: "镇或地区",
                areaCode: "地区编号",
                postalCode: "邮政编号",
                addressDetail: "地址详情",
                isDefault: "默认地址"
            }
            request.get('/address/add', { params }).then(res => {
                this.submitDetail = res.result
            })
        }
    }
};
</script>

<style scoped></style>